package com.bak;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TankFrame extends Frame {

    public static final int GAME_WIDTH = Integer.parseInt(PropertiesManager.getValue("GAME_WIDTH"));
    public static final int GAME_HEIGHT = Integer.parseInt(PropertiesManager.getValue("GAME_HEIGHT"));
    private Tank tank = new Tank(200, 200, Group.GOOD, false, this);
    private List<Bullet> bulletList = new ArrayList<>();
    private List<Tank> tankList = new ArrayList<>();
    private List<Expolode> expolodeList = new ArrayList<>();
    private Image image = null;

    public TankFrame() {
        setResizable(false);
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        addKeyListener(new MoveListener());

    }

    public List<Bullet> getBulletList() {
        return bulletList;
    }

    public List<Tank> getTankList() {
        return tankList;
    }

    @Override
    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.GREEN);
        g.drawString("当前子弹数量：" + bulletList.size(), 50, 50);
        g.drawString("当前敌人数量：" + tankList.size(), 50, 80);
        g.drawString("当前爆炸数量：" + expolodeList.size(), 50, 110);
        g.setColor(color);

        tank.paint(g); //绘制 主机坦克行动轨迹

        //绘制敌方坦克
        for (int i = 0; i < tankList.size(); i++) {
            tankList.get(i).paint(g);
        }

        Iterator<Bullet> bulletIterator = bulletList.iterator();
        //绘制所有发射子弹的行动轨迹
        while (bulletIterator.hasNext()) {
            Bullet next = bulletIterator.next();
            if (!next.isLive()) {
                bulletIterator.remove();
                continue;
            }
            next.paint(g);
        }


        //每次绘制完，检测是否存在子弹与坦克碰撞，碰撞两者消失。
        for (int j = 0; j < bulletList.size(); j++) {
            for (int i = 0; i < tankList.size(); i++) {
                bulletList.get(j).collideWith(tankList.get(i));
            }
        }


        for (int i = 0; i < expolodeList.size(); i++) {
            expolodeList.get(i).paint(g);
        }


    }

    /**
     * 双缓冲解决闪屏问题 ，双缓冲解决闪烁问题（利用一次性加载内存中的图片达到效果）
     *
     * @param g
     */
    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics graphics = image.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        graphics.setColor(color);
        paint(graphics);
        g.drawImage(image, 0, 0, null);

    }

    public List<Expolode> getExpolodeList() {
        return expolodeList;
    }


    class MoveListener extends KeyAdapter {
        /**
         * 记录哪个键被按下了
         */
        boolean bL = false;
        boolean bT = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bT = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
            }
            locateDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {

            int keyCode = e.getKeyCode();

            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bT = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;

                case KeyEvent.VK_CONTROL:
                    tank.fire();
                    break;
            }
            locateDir();

        }

        /**
         * 定位坦克的方向
         */
        void locateDir() {
            if (!bL && !bT && !bR && !bD) {
                tank.setMoving(false);
            } else {
                tank.setMoving(true);
                if (bL && !bT && !bR && !bD) tank.setDir(Dir.L);
                else if (bL && bT && !bR && !bD) tank.setDir(Dir.LU);
                else if (!bL && bT && !bR && !bD) tank.setDir(Dir.U);
                else if (!bL && bT && bR && !bD) tank.setDir(Dir.RU);
                else if (!bL && !bT && bR && !bD) tank.setDir(Dir.R);
                else if (!bL && !bT && bR && bD) tank.setDir(Dir.RD);
                else if (!bL && !bT && !bR && bD) tank.setDir(Dir.D);
                else if (bL && !bT && !bR && bD) tank.setDir(Dir.LD);

            }
        }


    }
}
